package com.gpzx.zx.module.mine.frame.view

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import com.gpzx.zx.R
import com.gpzx.zx.module.mine.ui.activity.CalendarActivity
import com.qmuiteam.qmui.widget.QMUITopBar
import com.wkq.base.frame.mosby.delegate.MvpView
import com.wkq.base.utils.TimeUtil
import com.wkq.database.utils.DataBaseUtils
import com.wu.common.utils.AlertUtil
import com.wu.common.utils.DateTimeUtil
import com.wu.common.utils.StatusBarUtil
import com.wu.common.utils.calendar.DPCManager
import com.wu.common.utils.calendar.DPDecor
import com.wu.common.utils.calendar.DPMode
import com.wu.common.utils.calendar.DatePicker
import java.util.*

/**
 * 作者: 吴奎庆
 *
 * 时间: 2020/7/24
 *
 * 简介:
 */
class CalendarView : MvpView, DatePicker.OnClickSignIn {
    val dpcManager by lazy {   DPCManager.getInstance()}

    var mActivity: CalendarActivity

    constructor(activity: CalendarActivity) {
        mActivity = activity
    }

    fun initView() {
      var times=  TimeUtil.getYearTime(System.currentTimeMillis(),"yyyy-M月-日")
       var mouth= DateTimeUtil.getMonth(System.currentTimeMillis())
       var year= DateTimeUtil.getYear(System.currentTimeMillis())
        var days= DateTimeUtil.getDays(System.currentTimeMillis())
        initToolBar("签到")
        dpcManager.clearnDATE_CACHE() //清除cache
        //自定义背景绘制示例
        val tmp: MutableList<String> = ArrayList()
       var info= DataBaseUtils.getSignInInfo(mActivity,year,mouth,days)
       if ( info!=null){
           mActivity.binding.myDatepicker.setRightTitle(true) //是否签到
       }else{
           mActivity.binding.myDatepicker.setRightTitle(false) //是否签到
       }

        DataBaseUtils.insertSigninInfo(mActivity,"2020-7-23","2020","7","22")
        var list =DataBaseUtils.getSignInInfos(mActivity ,year.toString(),mouth.toString())
        list.forEach { tmp.add(it.s_years_mounth_days) }
        dpcManager.setDecorBG(tmp) //预先设置日期背景 一定要在在开始设置
        mActivity.binding.myDatepicker.setDate(year.toInt(), mouth.toInt()) //设置日期
        mActivity.binding.myDatepicker.setMode(DPMode.SINGLE) //设置选择模式
        mActivity.binding.myDatepicker.setFestivalDisplay(true) //是否显示节日
        mActivity.binding.myDatepicker.setTodayDisplay(true) //是否高亮显示今天
        mActivity.binding.myDatepicker.setHolidayDisplay(true) //是否显示假期
        mActivity.binding.myDatepicker.setDeferredDisplay(false) //是否显示补休
        mActivity.binding.myDatepicker.setIsScroll(false) //是否允许滑动 false表示左右上下都不能滑动  单项设置上下or左右 你需要自己扩展
        mActivity.binding.myDatepicker.setIsSelChangeColor(true, mActivity.getResources().getColor(R.color.font_white_one)) //设置选择的日期字体颜色,不然有的背景颜色和默认的字体颜色不搭

        mActivity.binding.myDatepicker.setLeftTitle(mouth.toString() + "月") //左上方text

        mActivity.binding.myDatepicker.setOnClickSignIn(this) //点击签到事件


        //设置预先选中日期的背景颜色
        mActivity.binding.myDatepicker.setDPDecor(object : DPDecor() {
            override fun drawDecorBG(canvas: Canvas, rect: Rect, paint: Paint) {
                paint.color = mActivity.getResources().getColor(R.color.blue)
                canvas.drawCircle(rect.centerX().toFloat(), rect.centerY().toFloat(), rect.width() / 2.5f, paint)
            }
        })
        mActivity.binding.myDatepicker.setOnDatePickedListener(object : DatePicker.OnDatePickedListener {
            override fun onDatePicked(date: String?) {
                showMessage(message = date!!)
            }
        })

    }


    private fun initToolBar(title: String?) {
        StatusBarUtil.setStatusBarDarkMode(mActivity)
        StatusBarUtil.setColor(mActivity, mActivity!!.getResources().getColor(R.color.color_2b2b2b), 0)
        var toolbar = mActivity!!.binding.includeToolbar as QMUITopBar
        toolbar.setTitle(title)
        toolbar.setBackgroundColor(mActivity!!.resources.getColor(R.color.color_2b2b2b))
        toolbar.addLeftImageButton(R.drawable.ic_arrow_back_white_24dp, R.id.qmui_topbar_item_left_back).setOnClickListener { mActivity!!.finish() }

    }

    override fun signIn() {

        //动态更新的时候必须  清除cache
        //动态更新的时候必须  清除cache
        dpcManager.clearnDATE_CACHE() //清除cache

        //重新设置日期
        //重新设置日期
        var times=  TimeUtil.getYearTime(System.currentTimeMillis(),"yyyy-M月-日")
        val tmp: MutableList<String> = ArrayList()
        var mouth= DateTimeUtil.getMonth(System.currentTimeMillis())
        var year= DateTimeUtil.getYear(System.currentTimeMillis())
        var days= DateTimeUtil.getDays(System.currentTimeMillis())

        DataBaseUtils.insertSigninInfo(mActivity,times,year,mouth,days)
       var list =DataBaseUtils.getSignInInfos(mActivity ,year.toString(),mouth.toString())
        list.forEach { tmp.add(it.s_years_mounth_days) }
        dpcManager.setDecorBG(tmp)

        mActivity.binding.myDatepicker.setDate(year.toInt(), mouth.toInt())
        mActivity.binding.myDatepicker.setLeftTitle(mouth+"月")
        mActivity.binding.myDatepicker.setRightTitle(true)

        mActivity.binding.myDatepicker.setDPDecor(object : DPDecor() {
            override fun drawDecorBG(canvas: Canvas, rect: Rect, paint: Paint) {
                paint.color = mActivity.getResources().getColor(R.color.blue)
                canvas.drawCircle(rect.centerX().toFloat(), rect.centerY().toFloat(), rect.width() / 2.5f, paint)
            }
        })
        mActivity.binding.myDatepicker.invalidate() //刷新

    }
    fun showMessage(message:String){
        AlertUtil.showDeftToast(mActivity,message)
    }
}